//
//  ViewController.m
//  09-gcd的基本使用
//
//  Created by J on 16/2/25.
//  Copyright © 2016年 J. All rights reserved.
//

#import "ViewController.h"
@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{


//    [self syncMain];
//
//    dispatch_queue_t queue =  dispatch_queue_create("com.jzj", NULL);
//    dispatch_sync(queue, ^{
//        NSLog(@"1%@",[NSThread currentThread]);
//       dispatch_sync(queue, ^{
//           NSLog(@"2%@",[NSThread currentThread]);
//
//       });
//    });
    
    
    
    
    
}



//同步函数+主队列
- (void)syncMain{
    dispatch_queue_t queue = dispatch_get_main_queue();
    [self sync:queue];
    
}

//异步函数 + 主队列 只在主线程中执行任务
- (void)asynMain{
    dispatch_queue_t queue = dispatch_get_main_queue();//同步串行同步并发都一样 在主线程中进行

    [self a_sync:queue];
}


//同步 + 串行队列 不会开启新线程 在当前线程执行任务
- (void)syncSerial{
    
    
    
    dispatch_queue_t queue = dispatch_queue_create("com.jzj.www", DISPATCH_QUEUE_SERIAL);//同步串行同步并发都一样 在主线程中进行
    //2.将任务加入队列
    [self sync:queue];
   
}

//异步函数 + 串行队列 只会开一个新的子线程 按照顺序执行
- (void)asyncSerial{
    
    dispatch_queue_t queue = dispatch_queue_create("com.jzj.www", DISPATCH_QUEUE_SERIAL);
   
    [self a_sync:queue];
   
    
}

//同步函数+并发队列(同步线程不开线程 无法并发) 不能并发
- (void)synConcurrent{
    
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

    
    [self sync:queue];


}


- (void)asyncConcurrent{
    //1.获得全局并发队列
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    
    
    [self a_sync:queue];

    

}



//异步函数 并发队列 :可以同时开启多条线程
- (void)asyConcurrent{
    //1.创建一个并发队列
    
    dispatch_queue_t queue = dispatch_queue_create("com.jzj.www", DISPATCH_QUEUE_CONCURRENT);
    
 
    [self a_sync:queue];

}

- (void)a_sync:(dispatch_queue_t)queue{
    
    
    
    //2.将任务加入队列
    dispatch_async(queue, ^{
        for (NSInteger i = 0; i<10; i++) {
            NSLog(@"1%@",[NSThread currentThread]);
            
        }
        
    });
    dispatch_async(queue, ^{
        NSLog(@"2%@",[NSThread currentThread]);
        
    });
    dispatch_async(queue, ^{
        NSLog(@"%3@",[NSThread currentThread]);
        
    });  dispatch_async(queue, ^{
        NSLog(@"4%@",[NSThread currentThread]);
        
    });
    
}
- (void)sync:(dispatch_queue_t)queue{
    dispatch_sync(queue, ^{
        NSLog(@"1%@",[NSThread currentThread]);
        
    });
    
    
    dispatch_sync(queue, ^{
        NSLog(@"2%@",[NSThread currentThread]);
        
    });
    dispatch_sync(queue, ^{
        NSLog(@"3%@",[NSThread currentThread]);
        
    });
    dispatch_sync(queue, ^{
        NSLog(@"4%@",[NSThread currentThread]);
        
    });
    
}

@end
